/* File: DrawableDish.java
 * Purpose: Draws the basic environement to the screen
 * Author: Team Karp
 * Date: 1/29/08
 * Revisions: 1/29/08 - Initial Version - Adam Patterson
 * [2008-02-12, Ryan J. Bovorasmy] - Implemented the drawable dish, 
 *                                   added texture mapping, changed planes to
 *                                   ensure proper rendering. 
 *  [2008-02-15, Ryan J. Bovorasmy] Added comments (for adv. graphics report).
 *           
 * Additional Requirements: 
 */
package protovitae.engine;

import java.io.IOException;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

/**
 * Draws the environment to the screen
 * @author pattersa
 */
public class DrawableDish {

    private double radius;
    private double height;
    private final double CENTER_X = 0;
    private final double CENTER_Y = 0;
    private final double CENTER_Z = 0;

    private TextureLoader.Texture texture = null;
    private int joglTexture = -1;

    /**
     * Constructor
     * @param radius - sets the size of the dish
     */
    public DrawableDish(double radius, double height) {
        this.radius = radius;
        this.height = height;
        
        
        try {
            texture = TextureLoader.readTexture("image.jpg");
        } catch (IOException ex) {
            System.err.println("Unable to read texture.");
        }
    }
    
    /**
     * Draws this cyclinderical dish to the screen
     * @param gl - the context to draw to
     */
    public void render(GL gl) {
        gl.glEnable(GL.GL_TEXTURE_2D);
        GLU glu = new GLU();
        
        if(joglTexture<0){
            joglTexture = (texture != null)? texture.toGL(gl, glu, true): -1;
        }else{
           //already loaded 
        }

        //drawing settings
        gl.glPushAttrib(GL.GL_ENABLE_BIT // color_material
                | GL.GL_LIGHTING // material tracking 
                | GL.GL_CURRENT_BIT // color, normal
                | GL.GL_POLYGON_BIT); // front_face      
        
        gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR);
        gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE);
        gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT);
        
        gl.glEnable(GL.GL_COLOR_MATERIAL);

        gl.glLoadIdentity();
        GLUquadric quad = glu.gluNewQuadric();
        
        glu.gluQuadricDrawStyle(quad,GLU.GLU_FILL);
        glu.gluQuadricTexture(quad, true);
        
        gl.glFrontFace(GL.GL_CW);
        gl.glTranslated(0, 0, -50);
        
        gl.glBegin(GL.GL_QUADS);
        {
            gl.glNormal3f(0.0f, 1.0f, 0.0f);
            
            gl.glTexCoord2f(0.0f, 1.0f);
            gl.glVertex3f(-110.0f, 110.0f, -1.0f); // Top Left Of The Texture and Quad
            
            gl.glTexCoord2f(0.0f, 0.0f);
            gl.glVertex3f( 110.0f, 110.0f, -1.0f);  // Bottom Left Of The Texture and Quad
            
            gl.glTexCoord2f(1.0f, 0.0f);
            gl.glVertex3f(110.0f, -110.0f, -1.0f);   // Bottom Right Of The Texture and Quad
            
            gl.glTexCoord2f(1.0f, 1.0f);
            gl.glVertex3f(-110.0f, -110.0f, -1.0f);  // Top Right Of The Texture and Quad
        }
        gl.glEnd();
        
        gl.glPopAttrib();
    }
    
    
}
